package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.DriverInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.VehicleInfoMapper;
import com.ruoyi.system.domain.VehicleInfo;
import com.ruoyi.system.service.IVehicleInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车辆信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-05
 */
@Service
public class VehicleInfoServiceImpl implements IVehicleInfoService 
{
    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;

    /**
     * 查询车辆信息
     * 
     * @param id 车辆信息ID
     * @return 车辆信息
     */
    @Override
    public VehicleInfo selectVehicleInfoById(Long id)
    {
        return vehicleInfoMapper.selectVehicleInfoById(id);
    }

    /**
     * 查询车辆信息列表
     * 
     * @param vehicleInfo 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<VehicleInfo> selectVehicleInfoList(VehicleInfo vehicleInfo)
    {
        return vehicleInfoMapper.selectVehicleInfoList(vehicleInfo);
    }

    /**
     * 新增车辆信息
     * 
     * @param vehicleInfo 车辆信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int insertVehicleInfo(VehicleInfo vehicleInfo)
    {
        //车牌号
        String vehicleNumber = vehicleInfo.getVehicleNumber();
        VehicleInfo query = new VehicleInfo();
        query.setVehicleNumber(vehicleNumber);

        //如果数据库有这条数据，代表该驾驶员编号已经存在，则不能新增
        VehicleInfo selectDriverInfo = vehicleInfoMapper.selectVehicleInfo(query);
        if(!Objects.isNull(selectDriverInfo))
        {
            throw new BusinessException("该车牌号已存在，请你重新输入!");
        }
        //设置基本信息
        vehicleInfo.setStatus(1L);//新增完就默认待使用
        vehicleInfo.setIsDeleted(0L);
        vehicleInfo.setCreateBy(ShiroUtils.getLoginName());
        vehicleInfo.setCreateTime(new Date());
        vehicleInfo.setUpdateBy(ShiroUtils.getLoginName());
        vehicleInfo.setUpdateTime(new Date());
        return vehicleInfoMapper.insertVehicleInfo(vehicleInfo);
    }

    /**
     * 修改车辆信息
     * 
     * @param vehicleInfo 车辆信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int updateVehicleInfo(VehicleInfo vehicleInfo)
    {
        Long id = vehicleInfo.getId();
        VehicleInfo vehicleInfoById = vehicleInfoMapper.selectVehicleInfoById(id);
        //车牌号
        String vehicleNumber = vehicleInfo.getVehicleNumber();
        VehicleInfo query = new VehicleInfo();
        query.setVehicleNumber(vehicleNumber);

        //如果数据库有这条数据，代表该驾驶员编号已经存在，则不能更新
        VehicleInfo selectDriverInfo = vehicleInfoMapper.selectVehicleInfo(query);
        if(!Objects.isNull(selectDriverInfo)&&!Objects.equals(vehicleNumber,vehicleInfoById.getVehicleNumber()))
        {
            throw new BusinessException("该车牌号已存在，请你重新输入!");
        }
        vehicleInfo.setUpdateBy(ShiroUtils.getLoginName());
        vehicleInfo.setUpdateTime(new Date());
        return vehicleInfoMapper.updateVehicleInfo(vehicleInfo);
    }

    /**
     * 删除车辆信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteVehicleInfoByIds(String ids)
    {
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            VehicleInfo vehicleInfo = vehicleInfoMapper.selectVehicleInfoById(Long.parseLong(id));
            if(vehicleInfo.getStatus() == 2)
            {
                throw new BusinessException("该车辆"+vehicleInfo.getVehicleNumber()+"已被使用，无法删除!");
            }
        }
        return vehicleInfoMapper.deleteVehicleInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除车辆信息信息
     * 
     * @param id 车辆信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteVehicleInfoById(Long id)
    {
        VehicleInfo vehicleInfo = vehicleInfoMapper.selectVehicleInfoById(id);
        if(vehicleInfo.getStatus() == 2)
        {
            throw new BusinessException("该车辆"+vehicleInfo.getVehicleNumber()+"已被使用，无法删除!");
        }
        return vehicleInfoMapper.deleteVehicleInfoById(id);
    }
}
